package com.tyam.conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.tyam.bean.Empresa;
import com.tyam.dao.DAOFactory;


@FacesConverter(value="converteEmpresa", forClass=Empresa.class) 
public class ConverteEmpresa implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2.equals("0")){
			Empresa empresa = new Empresa();
			return empresa;
		}		
		return DAOFactory.getEmpresaDAO().getEmpresa(Integer.valueOf(arg2));
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		return String.valueOf(((Empresa) arg2).getCodigoEmpresa());
	}

}